package wc.Dao.DaoImpl;

import wc.Dao.AdminDao;
import wc.Utils.CyptoUtil;
import wc.Utils.JdbcUtil;
import wc.Utils.RandomUtil;
import wc.domain.Admin;
import wc.domain.History;
import wc.domain.User;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @ClassName:AdminDaoImpl
 * @Author: 鹿建文
 * @Date: 2022/4/6  - 10:22
 * @Describe:
 */
public class AdminDaoImpl implements AdminDao {

    @Override
    public Admin selectById(Integer id) throws SQLException {
        Admin admin = null;
        Connection conn = JdbcUtil.getConnection();
        String sql = "select * from tb_admin where id=?";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setInt(1, id);
        ResultSet rs = ps.executeQuery();
        while (rs.next()) {
            String account = rs.getString("account");
            String nikname = rs.getString("nikname");
            String passward = rs.getString("password");
            String salt = rs.getString("salt");
            admin = new Admin(id, nikname, account, passward, salt);
        }
        JdbcUtil.release(conn, ps, rs);
        return admin;
    }

    @Override
    public Admin selectByAccount(String account) throws SQLException {
        Admin admin = null;
        Connection conn = JdbcUtil.getConnection();
        String sql = "select * from tb_admin where account=?";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1, account);
        ResultSet rs = ps.executeQuery();
        while (rs.next()) {
            int id = rs.getInt("id");
            String nikname = rs.getString("nikname");
            String passward = rs.getString("password");
            String salt = rs.getString("salt");
            admin = new Admin(id, nikname, account, passward, salt);
        }
        JdbcUtil.release(conn, ps, rs);
        return admin;
    }

    @Override
    public boolean updatePassword(Integer id, String password) throws SQLException {
        Connection conn = JdbcUtil.getConnection();
        String sql = "update tb_admin set password =? where id=?";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1, password);
        ps.setInt(2, id);
        int res = ps.executeUpdate();
        JdbcUtil.release(conn, ps, null);
        return res == 1 ? true : false;
    }

    @Override
    public boolean updateSalt(Integer id, String salt) throws SQLException {
        Connection conn = JdbcUtil.getConnection();
        String sql = "update tb_admin set salt =? where id=?";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1, salt);
        ps.setInt(2, id);
        int res = ps.executeUpdate();
        JdbcUtil.release(conn, ps, null);
        return res == 1 ? true : false;
    }

    @Override
    public int updateAllSaltAndPassword() throws SQLException {
        int res = 0;
        Connection conn = JdbcUtil.getConnection();

        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery("select * from tb_admin");
        while (rs.next()) {
            int id = rs.getInt("id");
            String salt = rs.getString("salt");
            String password = rs.getString("password");

            String sql = "update tb_admin set salt = ? , password = ? where id = ?";
            PreparedStatement ps = conn.prepareStatement(sql);

            String newSalt = RandomUtil.genString(5, 16);
            ps.setString(1, newSalt);

            String oldPassword = CyptoUtil.decode(password, salt);
            String newPassword = CyptoUtil.encode(oldPassword, newSalt);
            ps.setString(2, newPassword);

            ps.setInt(3, id);

            ps.executeUpdate();
            res++;

            if (ps != null) {
                ps.close();
            }
        }

        JdbcUtil.release(conn, stmt, rs);
        return res;
    }

    @Override
    public boolean createUser(User user) throws SQLException {
        Connection conn = JdbcUtil.getConnection();
        String sql = "insert into tb_user (name,gender,tell,jifen )values (?,?,?,?);";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1, user.getName());
        ps.setBoolean(2, user.getGender());
        ps.setString(3, user.getTell());
        ps.setInt(4, user.getJifen());
        int rs = ps.executeUpdate();
        JdbcUtil.release(conn, ps, null);
        return rs==1? true:false;
//        String sqlh = "insert into tb_history (user_id,credits,credits_time,admin_id,op_time,reason )values (?,?,?,?,?,?);";
//        PreparedStatement psh = conn.prepareStatement(sqlh);
////        psh.setInt(1,user.setId());
//        psh.setInt(2,100);
//        java.util.Date date = new java.util.Date();
//
//        psh.setDate(3,DateTimeUtil.localDate2SqlDate(date) );
//        //        psh.setInt(4,user.setId());
//        psh.setDate(5,DateTimeUtil.localDate2SqlDate(date) );
//        psh.setString();

    }

    @Override
    public List<User> selectAllUsersDetailed() throws SQLException {
        ArrayList<User> res = new ArrayList<>();

        Connection conn = JdbcUtil.getConnection();
        Statement stmt = conn.createStatement();
        String sql = "select*from tb_user";
        ResultSet rs = stmt.executeQuery(sql);
        while (rs.next()) {
            int id = rs.getInt("id");
            String name = rs.getString("name");
            boolean gender = rs.getBoolean("gender");
            String tell = rs.getString("tell");
            int jifen = rs.getInt("jifen");
            User user = new User(id, name, gender, tell, jifen);
            res.add(user);
        }
        JdbcUtil.release(conn, stmt, rs);
        return res;
    }

    @Override
    public HashMap<Integer, List> selectAllUsersList() throws SQLException {
        HashMap<Integer, List> res = new HashMap<>();
        Connection conn = JdbcUtil.getConnection();
        Statement stmt = conn.createStatement();
        String sql = "select*from tb_user";
        ResultSet rs = stmt.executeQuery(sql);
        while (rs.next()) {
            int id = rs.getInt("id");
            String name = rs.getString("name");
            String tell = rs.getString("tell");
            List<String> tmp = new ArrayList<>();
            tmp.add(name);
            tmp.add(tell);
            res.put(id, tmp);
        }
        JdbcUtil.release(conn, stmt, rs);
        return res;
    }

    @Override
    public HashMap<Integer, List> selectUserCredits(String name) throws SQLException {
        HashMap<Integer, List> res = new HashMap<>();
        Connection conn = JdbcUtil.getConnection();
        String sql = "select*from tb_user where name=?";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1, name);
        ResultSet rs = ps.executeQuery();
        while (rs.next()) {
            int id = rs.getInt("id");
            int jifen = rs.getInt("jifen");
            ArrayList<String> tmp = new ArrayList<>();
            tmp.add(String.valueOf(jifen));
            tmp.add(name);
            res.put(id, tmp);
        }
        JdbcUtil.release(conn, ps, rs);
        return res;
    }

    @Override
    public ArrayList<History> selectUserCreditsHistorye(Integer userId) throws SQLException {
        ArrayList<History> res = new ArrayList<>();
        Connection conn = JdbcUtil.getConnection();
        String sql = "select*from tb_history where user_Id=?";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setInt(1, userId);
        ResultSet rs = ps.executeQuery();
        while (rs.next()) {
            int id = rs.getInt("id");
            int credits = rs.getInt("credits");
            Date creaditsTime = rs.getDate("credits_time");
            int adminId = rs.getInt("admin_id");
            Date opTime = rs.getDate("op_time");
            String reason = rs.getString("reason");
            History history = new History(id, userId, credits, creaditsTime, adminId, opTime, reason);
            res.add(history);
        }
        return res;
    }
}
